﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;

using Fuse8.DomainFramework.Common;
using Fuse8.DomainFramework.DataAccess.SqlDatabase;

using Fuse8.ContentManagementFramework.Domain;

namespace Fuse8.ContentManagementFramework.DataAccess.SqlAccess.Mappers
{
    internal class CmsMediaFileDataMapper : BaseSqlEntityDataMapper<CmsMediaFile, int>
    {
        private static BaseSqlEntityDataMapper<CmsMediaFile, int> _based = new BaseSqlEntityDataMapper<CmsMediaFile, int>(new string[] { "EntityMetaInformation", "SiteGroup", "Site", "SiteCategory", "MimeType", "IsPrimary", "BlockFromSearchEngines" });

        private static Dictionary<string, Func<object, IList<SqlParameter>>> _mappersTo = new Dictionary<string, Func<object, IList<SqlParameter>>>();

        static CmsMediaFileDataMapper()
        {
            _mappersTo.Add("cms_sp_MediaFileSelectOne",
                (p) =>
                {
                    return _based.MapTo("cms_sp_MediaFileSelectOne", p, false);
                });
            _mappersTo.Add("cms_sp_MediaFileAdd",
                (p) =>
                {
                    return _based.MapTo("cms_sp_MediaFileAdd", p, true);
                });
        }

        public CmsMediaFileDataMapper()
            : base()
        { }

        public override IList<SqlParameter> MapTo(string sp, object parameter, bool isForCreate)
        {
            Guard.ArgumentNotEmpty(sp, "sp");
            Guard.ArgumentNotNull(parameter, "parameter");

            return _mappersTo.ContainsKey(sp) ?
                _mappersTo[sp](parameter) :
                _based.MapTo(sp, parameter, isForCreate);
        }

        public override CmsMediaFile MapFrom(string sp, SqlDataReader reader)
        {
            Guard.ArgumentNotEmpty(sp, "sp");
            Guard.ArgumentNotNull(reader, "reader");

            var result = new CmsMediaFile
            {
                Id = reader.GetInt32(reader.GetOrdinal("MediaFileId")),
                SiteId = !reader.IsDBNull(reader.GetOrdinal("SiteId")) ?
                    reader.GetInt32(reader.GetOrdinal("SiteId")) :
                    new Nullable<int>(),
                SiteGroupId = !reader.IsDBNull(reader.GetOrdinal("SiteGroupId")) ?
                    reader.GetInt32(reader.GetOrdinal("SiteGroupId")) :
                    new Nullable<int>(),
                MimeTypeId = !reader.IsDBNull(reader.GetOrdinal("MimeTypeId")) ?
                    reader.GetInt32(reader.GetOrdinal("MimeTypeId")) :
                    new Nullable<int>(),
                OriginalFileName = reader.GetString(reader.GetOrdinal("OriginalFileName")),
                LinkedFileName = reader.GetString(reader.GetOrdinal("LinkedFileName")),
                FileSize = reader.GetInt64(reader.GetOrdinal("FileSize")),
                Height = !reader.IsDBNull(reader.GetOrdinal("Height")) ?
                   reader.GetInt32(reader.GetOrdinal("Height")) :
                   new Nullable<int>(),
                Width = !reader.IsDBNull(reader.GetOrdinal("Width")) ?
                    reader.GetInt32(reader.GetOrdinal("Width")) :
                    new Nullable<int>(),
                CreatedBy = reader.GetInt32(reader.GetOrdinal("CreatedBy")),
                DateCreated = reader.GetDateTime(reader.GetOrdinal("DateCreated")),
                LastUpdatedBy = !reader.IsDBNull(reader.GetOrdinal("LastUpdatedBy")) ?
                    reader.GetInt32(reader.GetOrdinal("LastUpdatedBy")) :
                    new Nullable<int>(),
                DateLastUpdated = !reader.IsDBNull(reader.GetOrdinal("DateLastUpdated")) ?
                   reader.GetDateTime(reader.GetOrdinal("DateLastUpdated")) :
                   new Nullable<DateTime>()
            };

            if (sp == "cms_sp_MediaFileSelectOne")
            {
                result.BlockFromSearchEngines = reader.GetBoolean(reader.GetOrdinal("BlockFromSearchEngines"));
            }

            if (result.MimeTypeId.HasValue)
            {
                result.MimeType = new CmsMimeType
                {
                    Id = reader.GetInt32(reader.GetOrdinal("MimeTypeId")),
                    ContentType = reader.GetString(reader.GetOrdinal("ContentType")),
                    MimeTypeName = reader.GetString(reader.GetOrdinal("MimeTypeName")),
                    IsImage = !reader.IsDBNull(reader.GetOrdinal("IsImage")) ?
                        reader.GetBoolean(reader.GetOrdinal("IsImage")) :
                        false
                };
            }

            return result;
        }
    }
}
